﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.IO
{
    public static class StreamExtention
    {
        /// <summary>
        /// 把流中所有的内容都以字符串的形式独取出来
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string ReadToEnd(this Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
        /// <summary>
        /// 获取流中的字节数组
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this Stream stream)
        {
            byte[] buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        /// <summary>
        /// 将字节数组写入流（从流的起始位置写，postion=0）
        /// </summary>
        /// <param name="strem"></param>
        /// <param name="buffer"></param>
        public static void Write(this Stream strem, byte[] buffer)
        {

            strem.Write(buffer, 0, buffer.Length);
        }
    }
}
